package com.penghaisoft.microwms.base.web.auth.controller;

import com.penghaisoft.microwms.base.web.auth.service.IBaseFactoryService;
import com.penghaisoft.microwms.common.base.model.*;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.dto.Transfer;
import com.penghaisoft.microwms.common.page.Pager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工厂管理
 */
@RestController
@RequestMapping(value = "/factory")
@Slf4j
public class BaseFactoryController extends BaseController {

	@Autowired
	private IBaseFactoryService baseFactoryService;

	
	/**
	 * 新增记录
	 * @param baseFactory
	 * @return
	 */
	@PostMapping("c")
	public TransResult create(BaseFactory baseFactory) {
		baseFactory.preInsert();
		baseFactory.setCreateBy(getLoginName());
		baseFactory.setLastModifiedBy(getLoginName());
		Resp resp =  baseFactoryService.create(baseFactory);
		return fromResp(resp);
	}

	/**
	 * 删除记录（逻辑删除active_flag=0)
	 * @param baseFactory
	 * @return
	 */
	@PostMapping("d")
	public TransResult delete(BaseFactory baseFactory) {
		baseFactory.preUpdate();
		Resp resp =  baseFactoryService.delete(baseFactory);
		return fromResp(resp);
	}

	/**
	 * 查询列表
	 * @param
	 * @return
	 */
	@PostMapping("list")
	public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, BaseFactory baseFactory) {
		Pager<BaseFactory> result = baseFactoryService.findListByCondition(page,rows,baseFactory);
		return success(result);
	}

	/**
	 * 查询单条
	 * @param id
	 * @return
	 */
	@GetMapping("r/{id}")
	public TransResult queryById(@PathVariable String id) {
		BaseFactory  baseFactory = baseFactoryService.findById(id);
		return success(baseFactory);
	}

	/**
	 * 修改记录
	 * @param baseFactory
	 * @return
	 */
	@PostMapping("u")
	public TransResult update(BaseFactory baseFactory) {
		baseFactory.preUpdate();
		baseFactory.setLastModifiedBy(getLoginName());
		Resp resp =  baseFactoryService.update(baseFactory);
		return fromResp(resp);
	}

	/**
	 * @Description:
	 * @author zxcq0
	 * @date 2017/3/23 17:59
	 * @param
	 * @return 返回所有的用户基本信息
	 */
	@GetMapping("/{factoryId}/user")
	public TransResult queryAllUser(@PathVariable String factoryId){
		BaseFactoryUserKey condition = new BaseFactoryUserKey();
		if (!factoryId.equals("all")){
			condition.setFactoryId(factoryId);
		}
		List<BaseFactoryUser> users = baseFactoryService.queryFactoryUserAll(condition);
		return success(users);
	}

	/**
	 * @Description: 给工厂分配人员
	 * @author zxcq0
	 * @date 2017/3/24 13:53
	 * @param
	 * @return
	 */
	@PostMapping("/u/user")
	public TransResult assignFactoryUsers(@RequestBody List<BaseFactoryUserKey> list){
		Resp resp = baseFactoryService.updateFactoryUser(list);
		return fromResp(resp);
	}
	
	/**
	 * 修改工厂资源关联信息
	 * @param transfer
	 * @return
	 */
	@PostMapping("u/res")
	public TransResult updateFacRes(@RequestBody Transfer<String, BaseFactoryResource> transfer) {
		Resp resp =  baseFactoryService.updateFacResRelation(transfer.getConditions(), transfer.getRecords());
		return fromResp(resp);
	}
	/**
	 * 查询全部工厂
	 * jzh
	 * @return
	 */
	 @GetMapping("all")
    public TransResult queryFactoryAll() {
        List<BaseFactory> factoryList = baseFactoryService.queryFactoryAll();
        return success(factoryList);
    }

	/**
	 * 根据角色Id查询角色资源关联信息
	 * @param id
	 * @return
	 */
	@GetMapping("r/res/{id}")
	public TransResult queryByRoleId(@PathVariable String id) {
		List<BaseFactoryResource> resRoleR = baseFactoryService.queryByFactoryId(id);
		return success(resRoleR);
	};
	
	
	/** 
	* @Title: queryFactoryType 
	* @Description: 查询工厂类型
	* @author zhangxu 
	* @2017年4月7日:2017年4月7日:上午11:19:35
	* @param @param baseFactory
	* @param @return    
	* @return TransResult    
	* @throws 
	*/
	@PostMapping("r/type")
	public TransResult queryFactoryType() {
		List<BaseFactory> factories = baseFactoryService.queryFactoryTypeAll();
		return success(factories);
	}
	
	/** 
	* @Title: queryFactoriesByType 
	* @Description: 查询加工工厂
	* @author pangxl
	* @2017年4月7日:2017年11月9日:上午17:19:35
	* @param @param baseFactory
	* @param @return    
	* @return TransResult    
	* @throws 
	*/
	@PostMapping("r/{factoryType}")
	public TransResult queryFactoriesByType(@PathVariable String factoryType) {
		List<BaseFactory> factories = baseFactoryService.queryFactoriesByType(factoryType);
		return success(factories);
	}
	
	/** 
	 * @Title querySapFactory
	 * @Description 根据factoryCode查询工厂、sap工厂关系
	 * @author luoteng
	 * @date 2017年6月27日:下午6:08:08
	 * @param factoryCode
	 * @return TransResult
	 */
	@GetMapping("/sap/{factoryCode}")
	public TransResult querySapFactory(@PathVariable String factoryCode){
//		List<BaseFactoryRsap> list = baseFactoryService.querySapFactory(factoryCode);
		return success();
	}
	
	/** 
	 * @Title updateSapFactory
	 * @Description 更新工厂、sap工厂关系
	 * @author luoteng
	 * @date 2017年6月28日:上午10:04:46
	 * @param list
	 * @param factoryCode
	 * @return Resp
	 */
//	@PostMapping("/sap/u/{factoryCode}")
//	public TransResult updateSapFactory(@RequestBody List<BaseFactoryRsap> list, @PathVariable String factoryCode){
//		baseFactoryService.delByFactoryCode(factoryCode);
//		if(null != list && !list.isEmpty()){
//			for(BaseFactoryRsap baseFactoryRsap : list){
//				baseFactoryRsap.preInsert();
//				baseFactoryService.InsertFactoryCode(baseFactoryRsap);
//			}
//		}
//		return success();
//	}

	/**
	 * 新增记录
	 * @param baseFactoryGroup
	 * @return
	 */
	@PostMapping("group/c")
	public TransResult createBaseFactoryGroup(BaseFactoryGroup baseFactoryGroup){
		baseFactoryGroup.preInsert();
		Resp resp =  new Resp();
		BaseFactoryGroup tmp = baseFactoryService.findByGroupCode(baseFactoryGroup.getGroupCode());
		if(tmp==null){
			resp = baseFactoryService.createBaseFactoryGroup(baseFactoryGroup);
		}else{
			resp.setMsg("base.factory.group.conflict");
		}
		return fromResp(resp);
	}

	
	/**
	 * 删除记录
	 * @param baseFactoryGroup
	 * @return
	 */
	@PostMapping("group/del")
	public TransResult deleteBaseFactoryGroup(BaseFactoryGroup baseFactoryGroup){
		baseFactoryGroup.preUpdate();
		Resp resp = baseFactoryService.deleBaseFactoryGroup(baseFactoryGroup);
		return fromResp(resp);
	}
	/**
	 * 查询列表
	 * @param page
	 * @param rows
	 * @param baseFactoryGroup
	 * @return
	 */
	@PostMapping("group/list")
	public  TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                             @RequestParam(name = "rows", defaultValue = "10") int rows, BaseFactoryGroup baseFactoryGroup){
		Pager<BaseFactoryGroup> result = baseFactoryService.findListByGroupCondition(page, rows, baseFactoryGroup);
		return success(result);
	}
	/**
	 * 根据ID查询
	 * @param id---baseFactoryGroup
	 * @return
	 */
	@GetMapping("group/{id}")
	public TransResult queryByGroupId(@PathVariable String id) {
		BaseFactoryGroup  baseFactory = baseFactoryService.findBaseFactoryGroupById(id);
		return success(baseFactory);
	}
	
	/**
	 * 修改更新
	 * @param baseFactoryGroup
	 * @return
	 */
	@PostMapping("group/update")
	public TransResult updateBaseFactoryGroup(BaseFactoryGroup baseFactoryGroup){
		baseFactoryGroup.preUpdate();
		Resp resp =  baseFactoryService.updateBaseFactoryGroup(baseFactoryGroup);
		return fromResp(resp);
	}
	
	/**
	 * 查询所有有效的工厂组
	 * 王治国 20171228
	 * @param 
	 * @return
	 */
	@PostMapping("getGroups")
	public TransResult getGroups() {
		List<BaseFactoryGroup> baseFactories = baseFactoryService.getGroups();
		return success(baseFactories);
	}

	

}
